﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UsageFileWorker;
using System.IO;
using Microsoft.SharePoint.Administration;

namespace UsageFileReader
{
    public partial class UsageDataTable : Form
    {
        IDictionary<int, SPUsageEntry> Entries;
        public UsageDataTable()
        {
            InitializeComponent();
            Entries = new Dictionary<int, SPUsageEntry>();
        }

        private void ofdUsageFile_FileOk(object sender, CancelEventArgs e)
        {
            dgvUsageEntries.Rows.Clear();
            Entries.Clear();
            dgvUsageEntries.Visible = true;

            Reader r = new Reader();
            r.UsageEntryAdded += new Reader.UsageEntryAddedHandler(r_UsageEntryAdded);
            r.ReadUsageLogFile(new FileInfo(ofdUsageFile.FileName));
        }

        void r_UsageEntryAdded(SPUsageEntry e)
        {
            AddDataRow(e);
        }



        private void btSelectFile_Click(object sender, EventArgs e)
        {
            ofdUsageFile.ShowDialog();
        }


        private void AddDataRow(SPUsageEntry e)
        {
            int newRowPosition = dgvUsageEntries.Rows.Add();
            Entries.Add(newRowPosition, e);
            DataGridViewRow row = dgvUsageEntries.Rows[newRowPosition];

            row.Cells["Type"].Value = GetDisplayNameOfType(e.GetType());
            row.Cells["CorrelationId"].Value = e.CorrelationId;
            row.Cells["FarmId"].Value = e.FarmId;
            row.Cells["MachineName"].Value = e.MachineName;
            row.Cells["ParentType"].Value = e.ParentType;
            row.Cells["ParentTypeGuid"].Value = e.ParentTypeGuid;
            row.Cells["SiteSubscriptionId"].Value = e.SiteSubscriptionId;
            row.Cells["Timestamp"].Value = e.Timestamp;
            row.Cells["TimestampUtc"].Value = e.TimestampUtc;
            row.Cells["User"].Value = e.User;
        }


        public static string GetDisplayNameOfType(Type eType)
        {

            if (eType == typeof(SPClickthroughUsageEntry))
            {
                return "Click Through";
            }

            else if (eType == typeof(SPFeatureUsageEntry))
            {
                return "Feature";
            }

            else if (eType == typeof(SPQueryUsageEntry))
            {
                return "Query";
            }

            else if (eType == typeof(SPRatingUsageEntry))
            {
                return "Rating";
            }

            else if (eType == typeof(SPRequestUsageEntry))
            {
                return "Request";
            }

            else if (eType == typeof(SPSiteInventoryUsageEntry))
            {
                return "Site Inventory";
            }

            else if (eType == typeof(SPTimerJobUsageEntry))
            {
                return "Timer Job";
            }

            else
            {
                return "Unknown";
            }

        }

        private void dgvUsageEntries_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                Details detailsForm = new Details(Entries[e.RowIndex]);
                detailsForm.Show(this);
            }
        }
    }
}
